import React from "react";
import { HashRouter, Route, Switch, Redirect } from "react-router-dom";
import { connect } from "react-redux";
import { getUserInfo } from "@/store/actions";
import Layout from "@/views/layout";
import Login from "@/views/login";
class Router extends React.Component {
    render() {
        const { token, role, getUserInfo } = this.props;
        return (
            <HashRouter>
                <Switch>
                    <Route exact path="/login" component={Login} />
                    <Route
                        path="/"
                        render={() => {
                            if (!token) {
                                return <Redirect to="/login" />;
                            } else {
                                if (role) {
                                    console.log('执行了该分支')
                                    return <Layout />;
                                } else {
                                    console.log("执行了else分支")
                                    getUserInfo(token).then(() => <Layout />);
                                }
                            }
                        }}
                    />
                </Switch>
            </HashRouter>
        );
    }
}

export default connect((state) => state.user, { getUserInfo })(Router);
